﻿using System;
using System.Net;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Text;

namespace RxjhServer
{
	public class WGSockClient : IDisposable
	{
		public Socket clientSocket;

		private RemoveWGClientDelegate removeFromTheServerList;

		private byte[] dataReceive;

		private bool disposed;

		[DllImport("PkEnBk.dll", CharSet = CharSet.Ansi)]
		public static extern uint send_en(Socket s, string astr1, int len, int flags);

		public IPAddress IP
		{
			get
			{
				try
				{
					IPAddress result;
					if (this.disposed)
					{
						result = null;
						return result;
					}
					result = ((IPEndPoint)this.clientSocket.RemoteEndPoint).Address;
					return result;
				}
				catch
				{
					this.Dispose();
				}
				return null;
			}
		}

		public WGSockClient(Socket from, RemoveWGClientDelegate rftsl)
		{
			this.dataReceive = new byte[102400];
			this.disposed = false;
			this.removeFromTheServerList = rftsl;
			this.clientSocket = from;
		}

		public void Dispose()
		{
			if (!this.disposed)
			{
				this.disposed = true;
				try
				{
					this.clientSocket.Shutdown(SocketShutdown.Both);
				}
				catch
				{
				}
				if (this.clientSocket != null)
				{
					this.clientSocket.Close();
				}
				this.clientSocket = null;
				if (this.removeFromTheServerList != null)
				{
					this.removeFromTheServerList(this);
				}
			}
		}

		public virtual void OnReceiveData(IAsyncResult ar)
		{
			try
			{
				if (!this.disposed)
				{
					int num = this.clientSocket.EndReceive(ar);
					if (num <= 0)
					{
						this.Dispose();
					}
					else
					{
						byte[] array = this.ProcessDataReceived(this.dataReceive, num);
						if (array != null)
						{
							this.clientSocket.BeginSend(array, 0, array.Length, SocketFlags.None, new AsyncCallback(this.OnSended), this);
						}
						else
						{
							this.clientSocket.BeginReceive(this.dataReceive, 0, this.dataReceive.Length, SocketFlags.None, new AsyncCallback(this.OnReceiveData), this);
						}
					}
				}
			}
			catch (Exception ex)
			{
				Console.WriteLine("{0}", ex.Message);
				this.Dispose();
			}
		}

		public void OnSended(IAsyncResult ar)
		{
			try
			{
				if (!this.disposed)
				{
					this.clientSocket.EndSend(ar);
					this.clientSocket.BeginReceive((ar.AsyncState as WGSockClient).dataReceive, 0, (ar.AsyncState as WGSockClient).dataReceive.Length, SocketFlags.None, new AsyncCallback(this.OnReceiveData), ar.AsyncState);
				}
			}
			catch
			{
			}
		}

		public void OnSended2(IAsyncResult ar)
		{
			try
			{
				if (!this.disposed)
				{
					this.clientSocket.EndSend(ar);
				}
			}
			catch (Exception)
			{
			}
		}

		public virtual byte[] ProcessDataReceived(byte[] data, int length)
		{
			return null;
		}

		public virtual byte[] DataReceived2(byte[] data, int length)
		{
			return null;
		}

		public virtual void Sendd(string str)
		{
			byte[] bytes = Encoding.Default.GetBytes(str);
			this.发送(bytes, bytes.Length);
		}

		public virtual void 发送(byte[] toSendBuff, int len)
		{
			try
			{
				byte[] array = new byte[len + 6];
				array[0] = 170;
				array[1] = 102;
				Buffer.BlockCopy(BitConverter.GetBytes(len), 0, array, 2, 4);
				Buffer.BlockCopy(toSendBuff, 0, array, 6, len);
				this.clientSocket.BeginSend(array, 0, len + 6, SocketFlags.None, new AsyncCallback(this.OnSended2), this);
			}
			catch (SocketException ex)
			{
				Form1.WriteLine(1, "帐号服务器 发送出错：" + ex.Message);
			}
			catch (Exception ex2)
			{
				Form1.WriteLine(1, "帐号服务器 发送出错：" + ex2.Message);
			}
		}

		public virtual void Send(byte[] toSendBuff, int len)
		{
			try
			{
				if (!this.disposed)
				{
					byte[] array = new byte[len];
					Buffer.BlockCopy(toSendBuff, 0, array, 0, len);
					this.clientSocket.BeginSend(array, 0, len, SocketFlags.None, new AsyncCallback(this.OnSended2), this);
				}
			}
			catch (Exception)
			{
			}
		}

		public virtual void Send(char[] toSendBuff, int len)
		{
			try
			{
				if (!this.disposed)
				{
					byte[] array = new byte[len];
					Buffer.BlockCopy(toSendBuff, 0, array, 0, len);
					this.clientSocket.BeginSend(array, 0, len, SocketFlags.None, new AsyncCallback(this.OnSended2), this);
				}
			}
			catch (Exception)
			{
			}
		}

		public virtual void Send(byte[] toSendBuff, int offset, int len)
		{
			try
			{
				if (!this.disposed)
				{
					byte[] array = new byte[len];
					Buffer.BlockCopy(toSendBuff, offset, array, 0, len);
					if (!this.disposed)
					{
						this.clientSocket.BeginSend(array, 0, len, SocketFlags.None, new AsyncCallback(this.OnSended2), this);
					}
				}
			}
			catch (Exception)
			{
			}
		}

		public void Start()
		{
			this.clientSocket.BeginReceive(this.dataReceive, 0, this.dataReceive.Length, SocketFlags.None, new AsyncCallback(this.OnReceiveData), this);
		}
	}
}
